package com.mrlv.rabbitmq;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author Mrlv
 * @version 1.0
 * @description:
 * 异步回调
 * @date 2021年11月19日 18:14
 */
public class CompletableFutureDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "没有返回值， update mysql ok");
        });

        voidCompletableFuture.get();

        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "有返回值， update mysql ok");
            //int a = 10/0;
            return 1024;
        });

        Integer resultCode = integerCompletableFuture.whenComplete((r, u) -> {
            System.out.println("===========TRUE-R:" + r);
            System.out.println("===========TRUE-U:" + u);
        }).exceptionally(e -> {
            System.out.println("===========ERROR:" + e.getMessage());
            return 500;
        }).get();

        System.out.println(resultCode);
    }
}
